<?php

require_once 'init.php';
require_once cf\Config::path.'mail.php';
require_once 'recaptchalib.php';
$privatekey = '6Lcw_eUSAAAAAHo8wA3cDkgq2lDupUhI8mcUtXHk';

$page = cf\api\page\get('feedback', 'main_menu');
$smarty->assign('page',$page);
$smarty->assign('pageText',cf\api\page\getText($page['id']));
$pageId = $page['id'];

if (count($page['branch']) > 1) {
	$smarty->assign('childPages', cf\api\page\getTree($page['parent']['id'],false,1,$page['branch'][1]['path']));
}

$ok = false;

if (cf\param::exist('name')) {
	$name = cf\param::get('name');
	$company = cf\param::get('company');
	$email = cf\param::get('email');
	$phone = cf\param::get('phone');
	$message = cf\param::get('message');
	
	$txt = "
		ФИО: $name <br>\n
		Организация: $company <br>\n
		E-mail: $email <br>\n
		Телефон: $phone <br>\n
		Вопрос: $message <br>\n
	";
	
	$resp = recaptcha_check_answer(
		$privatekey,
		$_SERVER["REMOTE_ADDR"],
		$_POST["recaptcha_challenge_field"],
		$_POST["recaptcha_response_field"]
	);
	
	if ($resp->is_valid) {
		$msg = new cf\Mail('Сообщение с сайта Мастер-Домино',$txt);
		try {
			cf\Mail::send($admin->email(),$msg);
		} catch(Exception $e) {
			print_r($e);
			exit;
		}
		$ok = true;
	} else {
		$ok = false;
		$smarty->assign('error','Символы с картнки введены неправильно');
	}
	
	$smarty->assign('msgName',$name);
	$smarty->assign('msgCompany',$company);
	$smarty->assign('msgEmail',$email);
	$smarty->assign('msgPhone',$phone);
	$smarty->assign('msgMessage',$message);
}

$smarty->assign('ok',$ok);

show(
	'feedback', 
	$page['name'] ? $page['name'] : $page['menu_name'], 
	getCrumbs($page['branch']), 
	$page['title'], 
	$page['keywords'], 
	$page['descr']
);
?>